package games

import (
	"wanyu/server/api/gameapi/gameframe"
	"wanyu/server/api/gameapi/games/common"
	"wanyu/server/api/gameapi/games/ddz"
	"wanyu/server/api/gameapi/games/fkddz"
	_ "wanyu/server/api/gameapi/games/fkddz"
	"wanyu/server/api/gameapi/games/shby"
	"wanyu/server/api/gameapi/games/xlch"
	"wanyu/server/api/gameapi/games/xzmj"
	"wanyu/server/api/gameapi/games/xzdd"
	_ "wanyu/server/api/gameapi/games/xzdd"
	"wanyu/server/api/gameapi/games/zjh"

	//"windplatform/server/api/gameapi/games/dfdc"
	//"windplatform/server/api/gameapi/games/hhdz"
	//"windplatform/server/api/gameapi/games/jdnn"
	//"windplatform/server/api/gameapi/games/lhd"
	//"windplatform/server/api/gameapi/games/qznn"
	//"windplatform/server/api/gameapi/games/shby"
	errors "wanyu/server/engine/wperror"
	"wanyu/server/protogo/public"

	b3core "github.com/magicsea/behavior3go/core"
)

var gameFactory = make(map[public.GameType]gameframe.GameFactory)

func getFactory(gameType public.GameType) gameframe.GameFactory {
	factory, _ := gameFactory[gameType]
	if nil == factory {
		switch gameType {
		case public.GameType_XZMJ:
			gameFactory[public.GameType_XZMJ] = xzmj.Init()
		case public.GameType_XLCH:
			gameFactory[public.GameType_XLCH] = xlch.Init()
		case public.GameType_BRNN:
		case public.GameType_DDZ:
			gameFactory[public.GameType_DDZ] = ddz.Init()
		case public.GameType_DZPK:
		case public.GameType_EBG:
		case public.GameType_ERMJ:
		case public.GameType_HBSL:
		//case public.GameType_HHDZ:
		//	gameFactory[public.GameType_HHDZ] = hhdz.Init()
		//case public.GameType_JDNN:
		//	gameFactory[public.GameType_JDNN] = jdnn.Init()
		//case public.GameType_LHD:
		//	gameFactory[public.GameType_LHD] = lhd.Init()
		case public.GameType_PDK:
		//case public.GameType_QZNN:
		//	gameFactory[public.GameType_QZNN] = qznn.Init()
		case public.GameType_SHBY:
			gameFactory[public.GameType_SHBY] = shby.Init()
		case public.GameType_ZJH:
			gameFactory[public.GameType_ZJH] = zjh.Init()
		//case public.GameType_DFDC:
		//	gameFactory[public.GameType_DFDC] = dfdc.Init()

		case public.GameType_XZDD:
			gameFactory[public.GameType_XZDD] = xzdd.Init()
		case public.GameType_FKDDZ:
			gameFactory[public.GameType_FKDDZ] = fkddz.Init()
		default:
			errors.Check(false, errors.ErrorParam, "un support game type")
		}
		fact, _ := gameFactory[gameType]
		errors.Check(nil != fact, errors.ErrorGameType, "game factory is nil")
		return fact
	} else {
		return factory
	}
}

func CreateGame(gameType public.GameType, room gameframe.Room) gameframe.Game {
	return getFactory(gameType).CreateGame(room)
}

func CreateBehaviorTree(gameType public.GameType) *b3core.BehaviorTree {
	//每次生成树都必须调用
	//创建一个临时机器人来初始化决策树的通用节点
	fact := getFactory(gameType)
	common.Init(fact.CreateRobot(nil, nil, nil, nil))
	return fact.CreateBehaviorTree()
}

func CreateRobot(gameType public.GameType, mgr gameframe.RobotMgr, player gameframe.Player, tree *b3core.BehaviorTree, rCfg *gameframe.RobotCfg) gameframe.Robot {
	return getFactory(gameType).CreateRobot(mgr, player, tree, rCfg)
}

func CreateExCfgBuf(gameType public.GameType) (interface{}, interface{}) {
	return getFactory(gameType).CreateExCfgBuf()
}
